/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.logger;


/**
 * This class manages logging messages for the Java classes that are not TelosysObject descendent. <br>
 * 
 * Same principle as the trace management in TelosysObject <br>
 * but designed to be held as a static private attribute in the class that used it.
 * 
 * @author Laurent GUERIN
 *
 */
public class TelosysClassLogger 
{
    
    private String   _sClassName             = "???";
    
    private int      _iLastTraceConfigState  = -1 ; 
    
    private boolean  _bTrace                 = false ; // true if trace is activated for this classname


    //-----------------------------------------------------------------------------
    /**
     * Constructs a logs manager instance dedicated for the given Java class
     * @param cl the class to managed
     */
    public TelosysClassLogger(Class<?> cl)
    {
        if ( cl != null )
        {
            _sClassName = cl.getName() ;            
        }
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Constructs a logs manager instance dedicated for the given Java class
     * @param cl the class to managed
     */
    public TelosysClassLogger(Class<?> cl, boolean traceFlag)
    {
        if ( cl != null )
        {
            _sClassName = cl.getName() ;
            _bTrace = traceFlag ;
        }
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Logs a standard TRACE message, using the active loggers, <br>
     * only if the trace flag is "true" for the Java class managed
     * 
     * @param s the message to log
     */
    public void trace(String s)
    {
        //--- If the configuration has changed since the last acces ...
        if ( TraceConfig.getCurrentState() != _iLastTraceConfigState )
        {
          //--- Refresh the flag from the configuration
          _bTrace = TraceConfig.flagIsOn(_sClassName);
          //--- Keep the current configuration state
          _iLastTraceConfigState = TraceConfig.getCurrentState();
        }
        if ( _bTrace == true )
        {
            TelosysLogger.trace ( _sClassName, s );
        }        
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Logs a standard ERROR message, using the active loggers
     * @param s the message to log
     */
    public void error(String s)
    {
        TelosysLogger.error ( _sClassName, s );
    }
    //-----------------------------------------------------------------------------
    /**
     * Logs the given exception, using the active loggers
     * 
     * @param exception : the exception to log
     */
    public void error(Throwable exception)
    {
        TelosysLogger.error( _sClassName, exception );
    }

    //-----------------------------------------------------------------------------
    /**
     * Logs the given ERROR message and exception, using the active loggers
     * @param sMsg : the error message 
     * @param exception : the exception
     */
    public void error(String sMsg, Throwable exception)
    {
        this.error(sMsg);
        this.error(exception);
    }

    //-----------------------------------------------------------------------------
    /**
     * Logs a standard INFO message, using the active loggers
     * @param s the message to log
     */
    public void info(String s)
    {
        TelosysLogger.info ( _sClassName, s );
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Logs a standard WARN message, using the active loggers
     * 
     * @param s the message to log
     */
    public void warn(String s)
    {
        TelosysLogger.warn( _sClassName, s);
    }
    
}